package per.sen.shopping.infrastructure.common.exception;


import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveViolationException;
import io.smallrye.mutiny.Uni;
import jakarta.annotation.Priority;
import jakarta.el.MethodNotFoundException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
import jakarta.ws.rs.NotAllowedException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import per.sen.shopping.infrastructure.common.response.ApiResponse;
import per.sen.shopping.infrastructure.common.response.CustomCode;

import per.sen.shopping.infrastructure.common.response.HttpStatus;

import java.net.BindException;

/**
 * @Describe: 全局异常拦截器
 * @Author: LYS
 * @Date: 2024/6/1 16:53
 */
@Provider
public class ExceptionMappers   {
    /**
     * @Describe: 基础异常
     * @Author: LYS
     * @Date: 2024/6/1 17:32
     */
   
    @ServerExceptionMapper(BaseException.class)
    public RestResponse<ApiResponse> mapException  (BaseException baseException) {
        return RestResponse.status(Response.Status.INTERNAL_SERVER_ERROR, ApiResponse.error(baseException.getCode(),baseException.getModule()+":"+baseException.getDefaultMessage()));
    }

    /**
     * @Describe: 业务异常
     * @Author: LYS
     * @Date: 2024/6/1 17:27
     */
  
    @ServerExceptionMapper(CustomException.class)
    public RestResponse<ApiResponse> mapException  (CustomException customException) {
        return RestResponse.status(Response.Status.BAD_REQUEST, ApiResponse.error(customException.getCode(),customException.getErrorMsg()));
    }

    /**
     * @Describe: 参数校验异常
     * @Author: LYS
     * @Date: 2024/6/1 17:27
     */
   
    @ServerExceptionMapper(ResteasyReactiveViolationException.class)
    public Uni<RestResponse<ApiResponse>> mapException (ResteasyReactiveViolationException  x) {
        return  Uni.createFrom().item(RestResponse.status(Response.Status.BAD_REQUEST, ApiResponse.error(CustomCode.ILLEGAL_PARAM_EXCEPTION.getCode(), x.getMessage().split(":")[1].trim())));
    }

    /**
     * @Describe: 请求路径异常
     * @Author: LYS
     * @Date: 2024/6/1 18:01
     */
    @ServerExceptionMapper(NotFoundException.class)
    public Uni<RestResponse<ApiResponse>> mapException (NotFoundException  notFoundException) {
        return  Uni.createFrom().item(RestResponse.status(Response.Status.NOT_FOUND, ApiResponse.error(HttpStatus.NOT_FOUND.getCode(),HttpStatus.NOT_FOUND.getMsg())));
    }

    /**
     * @Describe: 请求方式异常
     * @Author: LYS
     * @Date: 2024/6/1 18:01
     */
    @ServerExceptionMapper(NotAllowedException.class)
    public Uni<RestResponse<ApiResponse>> mapException (NotAllowedException notAllowedException) {
        return Uni.createFrom().item(RestResponse.status(Response.Status.METHOD_NOT_ALLOWED, ApiResponse.error(HttpStatus.METHOD_NOT_ALLOWED.getCode(),HttpStatus.METHOD_NOT_ALLOWED.getMsg())));
    }



}
